import { addPaymentSerialApi } from "@/api/financialManagement/payableManagement/paymentRegistration";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import dayjs from "dayjs";
import { Ref } from "vue";
export default function OperationHook(state: { formState: PaymentRegistration.AddPaymentWater; formDataRef: Ref<any>; emit: any; loading: Ref<boolean> }) {
  const { formState, formDataRef, emit, loading } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const submitHandle = () => {
    formDataRef.value.validate().then(() => {
      const params: PaymentRegistration.AddPaymentWater = {
        ...formState,
        payTime: dayjs(formState.payTime).format("YYYY-MM-DD HH:mm:ss"),
        payAccountId: formState.payAccountId[1]
      };
      loading.value = true;
      addPaymentSerialApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("添加成功!");
            emit("submit");
          }
        })
        .finally(() => (loading.value = false));
    });
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    submitHandle,
    resetHandle,
    resetFields,
    validateZhInputHandle
  };
}
